Reading files

The iterator notation is easiest.


In [4]:
f = open('kaiju_movies.dat')
for movie in f:
    print movie,
f.close()


Godzilla vs. Destoroyah|1995|Destoroyah,Godzilla Junior
Godzilla|1998|Matthew Broderick
The Return of Godzilla|1984|Shockirus
Godzilla|1954|None
Godzilla Raids Again|1955|Anguiras
King Kong vs. Godzilla|1962|King Kong,giant octopus
Ghidorah, the Three-Headed Monster|1964|King Ghidorah,Mothra,Rodan
Godzilla vs. the Sea Monster|1966|Ebirah,Mothra,giant condor
Son of Godzilla|1967|Kamacuras,Kumonga,Minilla
Destroy All Monsters|1968|Anguiras,Baragon,Gorosaurus,King Ghidorah,Kumonga,Manda,Minilla,Mothra,Rodan,Varan
All Monsters Attack|1969|Gabara,Minilla,Maneater
Godzilla vs. Hedorah|1971|Hedorah
Godzilla vs. Megalon|1973|Gigan,Jet Jaguaar,Megalon
Godzilla vs. Mechagodzilla|1974|Anguiras,King Caesar,Mechagodzilla,Fake Godzilla
Terror of Mechagodzilla|1975|Mechagodzilla,Titanosaurus
Godzilla vs. Biollante|1989|Biollante
Godzilla vs. King Ghidorah|1991|Dorat,Godzillasaurus,King Ghidorah,Mecha-King Ghidorah
Godzilla vs. Mothra|1992|Battra,Mothra
Godzilla vs. Mechagodzilla II|1993|Baby Godzilla,Rodan,Mechagodzilla,Mecha-King Ghidorah
Godzilla vs. SpaceGodzilla|1994|Fairy Mothra,Little Godzilla,MOGUERA,SpaceGodzilla
Godzilla 2000: Millennium|1999|Orga
Godzilla vs. Megaguiras|2000|Meganulon,Meganula,Megaguiras
Godzilla, Mothra, and King Ghidorah: Giant Monsters All-Out Attack|2001|Baragon,King Ghidorah,Mothra
Godzilla Against Mechagodzilla|2002|Kiryu
Godzilla: Tokyo S.O.S.|2003|Kamoebas,Kiryu,Mothra
Godzilla: Final Wars|2004|Anguiras,Ebirah,Gigan,Hedorah,Monster X,Keizer Ghidorah,Kamacuras,King Caesar,Kumonga,Manda,Minilla,Mothra,Rodan,Zilla
Godzilla|2014|M.U.T.O.
Mothra vs. Godzilla|1964|Mothra
Invasion of Astro-Monster|1965|King Ghidorah,Rodan
Godzilla vs. Gigan|1972|Anguiras,Gigan,King Ghidorah

(The comma at the end suppresses extra newline). Can also use the object-oriented interface.


In [5]:
f = file('kaiju_movies.dat')
for movie in f:
    print movie,
f.close()


Godzilla vs. Destoroyah|1995|Destoroyah,Godzilla Junior
Godzilla|1998|Matthew Broderick
The Return of Godzilla|1984|Shockirus
Godzilla|1954|None
Godzilla Raids Again|1955|Anguiras
King Kong vs. Godzilla|1962|King Kong,giant octopus
Ghidorah, the Three-Headed Monster|1964|King Ghidorah,Mothra,Rodan
Godzilla vs. the Sea Monster|1966|Ebirah,Mothra,giant condor
Son of Godzilla|1967|Kamacuras,Kumonga,Minilla
Destroy All Monsters|1968|Anguiras,Baragon,Gorosaurus,King Ghidorah,Kumonga,Manda,Minilla,Mothra,Rodan,Varan
All Monsters Attack|1969|Gabara,Minilla,Maneater
Godzilla vs. Hedorah|1971|Hedorah
Godzilla vs. Megalon|1973|Gigan,Jet Jaguaar,Megalon
Godzilla vs. Mechagodzilla|1974|Anguiras,King Caesar,Mechagodzilla,Fake Godzilla
Terror of Mechagodzilla|1975|Mechagodzilla,Titanosaurus
Godzilla vs. Biollante|1989|Biollante
Godzilla vs. King Ghidorah|1991|Dorat,Godzillasaurus,King Ghidorah,Mecha-King Ghidorah
Godzilla vs. Mothra|1992|Battra,Mothra
Godzilla vs. Mechagodzilla II|1993|Baby Godzilla,Rodan,Mechagodzilla,Mecha-King Ghidorah
Godzilla vs. SpaceGodzilla|1994|Fairy Mothra,Little Godzilla,MOGUERA,SpaceGodzilla
Godzilla 2000: Millennium|1999|Orga
Godzilla vs. Megaguiras|2000|Meganulon,Meganula,Megaguiras
Godzilla, Mothra, and King Ghidorah: Giant Monsters All-Out Attack|2001|Baragon,King Ghidorah,Mothra
Godzilla Against Mechagodzilla|2002|Kiryu
Godzilla: Tokyo S.O.S.|2003|Kamoebas,Kiryu,Mothra
Godzilla: Final Wars|2004|Anguiras,Ebirah,Gigan,Hedorah,Monster X,Keizer Ghidorah,Kamacuras,King Caesar,Kumonga,Manda,Minilla,Mothra,Rodan,Zilla
Godzilla|2014|M.U.T.O.
Mothra vs. Godzilla|1964|Mothra
Invasion of Astro-Monster|1965|King Ghidorah,Rodan
Godzilla vs. Gigan|1972|Anguiras,Gigan,King Ghidorah

Reading all of the lines at once


In [6]:
f = open('kaiju_movies.dat')
movies = f.readlines()
print movies
f.close()


['Godzilla vs. Destoroyah|1995|Destoroyah,Godzilla Junior\n', 'Godzilla|1998|Matthew Broderick\n', 'The Return of Godzilla|1984|Shockirus\n', 'Godzilla|1954|None\n', 'Godzilla Raids Again|1955|Anguiras\n', 'King Kong vs. Godzilla|1962|King Kong,giant octopus\n', 'Ghidorah, the Three-Headed Monster|1964|King Ghidorah,Mothra,Rodan\n', 'Godzilla vs. the Sea Monster|1966|Ebirah,Mothra,giant condor\n', 'Son of Godzilla|1967|Kamacuras,Kumonga,Minilla\n', 'Destroy All Monsters|1968|Anguiras,Baragon,Gorosaurus,King Ghidorah,Kumonga,Manda,Minilla,Mothra,Rodan,Varan\n', 'All Monsters Attack|1969|Gabara,Minilla,Maneater\n', 'Godzilla vs. Hedorah|1971|Hedorah\n', 'Godzilla vs. Megalon|1973|Gigan,Jet Jaguaar,Megalon\n', 'Godzilla vs. Mechagodzilla|1974|Anguiras,King Caesar,Mechagodzilla,Fake Godzilla\n', 'Terror of Mechagodzilla|1975|Mechagodzilla,Titanosaurus\n', 'Godzilla vs. Biollante|1989|Biollante\n', 'Godzilla vs. King Ghidorah|1991|Dorat,Godzillasaurus,King Ghidorah,Mecha-King Ghidorah\n', 'Godzilla vs. Mothra|1992|Battra,Mothra\n', 'Godzilla vs. Mechagodzilla II|1993|Baby Godzilla,Rodan,Mechagodzilla,Mecha-King Ghidorah\n', 'Godzilla vs. SpaceGodzilla|1994|Fairy Mothra,Little Godzilla,MOGUERA,SpaceGodzilla\n', 'Godzilla 2000: Millennium|1999|Orga\n', 'Godzilla vs. Megaguiras|2000|Meganulon,Meganula,Megaguiras\n', 'Godzilla, Mothra, and King Ghidorah: Giant Monsters All-Out Attack|2001|Baragon,King Ghidorah,Mothra\n', 'Godzilla Against Mechagodzilla|2002|Kiryu\n', 'Godzilla: Tokyo S.O.S.|2003|Kamoebas,Kiryu,Mothra\n', 'Godzilla: Final Wars|2004|Anguiras,Ebirah,Gigan,Hedorah,Monster X,Keizer Ghidorah,Kamacuras,King Caesar,Kumonga,Manda,Minilla,Mothra,Rodan,Zilla\n', 'Godzilla|2014|M.U.T.O.\n', 'Mothra vs. Godzilla|1964|Mothra\n', 'Invasion of Astro-Monster|1965|King Ghidorah,Rodan\n', 'Godzilla vs. Gigan|1972|Anguiras,Gigan,King Ghidorah\n']

Writing files

Writing one line at a time


In [7]:
dumb_monsters = ('Hedorah', 'Megalon', 'Gigan', 'Minilla')
f = open('monsters.txt', 'w')
for monster in dumb_monsters:
    f.write(monster + '\n')
f.close()

Writing all of the lines at once

This approach does not add newlines, so add them yourself if needed.


In [2]:
dumb_monsters = ('Hedorah', 'Megalon', 'Gigan', 'Minilla')
f = open('monsters2.txt', 'w')
f.writelines(dumb_monsters)
f.close()

Binary files

Open and close similar to text files, but use read() and write().


In [8]:
f = open('nikki.jpg', 'rb')
my_dog = f.read()
f.close()
# Do arbitrary stuff with data.
f = open('new_nikki.jpg', 'wb')
f.write(my_dog)
f.close()

Format-specific binary I/O is available through standard modules, like Image.


In [9]:
from IPython.display import Image
puppeh = Image(filename = 'nikki.jpg')
puppeh


Out[9]:

Pickels!

The pickle is an internal Python format for writing arbitrary data to a file in a way that allows it to be read in again, intact.


In [10]:
movies = [{'title': 'Godzilla', 'year': 1954}, {'title': 'Godzilla 2000: Millennium', 'year': 1999}]
import pickle
f = open('pickled_kaiju.pkl', 'wb')
pickle.dump(movies, f)
f.close()

In [11]:
f = open('pickled_kaiju.pkl', 'rb')
pickled_movies = pickle.load(f)
f.close()
print pickled_movies


[{'title': 'Godzilla', 'year': 1954}, {'title': 'Godzilla 2000: Millennium', 'year': 1999}]

More

There is much more to file I/O in Python - binary data, reading and writing through network connections, other object serialization methods. Dive into that when you are comfortable with basic text input and output.